package com.theyeasy.dvmini.service.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.google.common.base.Strings;
import com.theyeasy.dvmini.dao.VankerDao;
import com.theyeasy.dvmini.model.Sales;
import com.theyeasy.dvmini.model.Vanker;
import com.theyeasy.dvmini.service.VankerService;
import com.theyeasy.dvmini.util.CommonUtil;
import com.theyeasy.dvmini.util.PageResults;
import com.theyeasy.dvmini.util.StringUtil;

@Service
public class VankerServiceImpl implements VankerService {

    @Autowired
    VankerDao vankerDao;

    @Override
    public PageResults<Vanker> getList(Integer page) {
        Example example = new Example(Vanker.class);
        example.setOrderByClause("id desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleted", false);
        RowBounds row=new RowBounds((page-1)*5,5);
        List<Vanker> list = vankerDao.selectByExampleAndRowBounds(example,row);
        int total=vankerDao.selectCountByExample(example);
        PageResults<Vanker> pageresult = new PageResults<>();
        pageresult.setPage(page);
        pageresult.setPageSize(5);
        pageresult.setRows(list);
        pageresult.setTotal(total);
        return pageresult;
    }

    /*
     * (非 Javadoc) Description:
     * @see com.theyeasy.dvmini.service.VankerService#addVanker(java.lang.String, java.lang.String, boolean)
     */
    @Override
    public String addVanker(String name, String phone) {
        // TODO Auto-generated method stub
        if (Strings.isNullOrEmpty(name))
            return "名称不能为空";
        if (Strings.isNullOrEmpty(phone))
            return "手机不能为空";
        try {
            Vanker vanker = new Vanker();
            vanker.setDeleted(false);
            vanker.setName(name);
            vanker.setPhone(phone);
            vanker.setCreatedtime(CommonUtil.getTime());
            vanker.setIsvalid(true);
            vankerDao.insertSelective(vanker);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "添加失败，" + e.getMessage();
        }
        return "ok";
    }

    /*
     * (非 Javadoc) Description:
     * @see com.theyeasy.dvmini.service.VankerService#deleteVanker(java.lang.Integer)
     */
    @Override
    public String deleteVanker(Integer id) {
        // TODO Auto-generated method stub
        if (StringUtil.isZearoOrNull(id))
            return "id 不能为空";
        Vanker vanker = vankerDao.selectByPrimaryKey(id);
        if (vanker == null)
            return "万小二不存在";
        vanker.setDeleted(true);
        int row = vankerDao.updateByPrimaryKey(vanker);
        return row > 0 ? "ok" : "删除失败";
    }

    /*
     * (非 Javadoc) Description:
     * @see com.theyeasy.dvmini.service.VankerService#updateVanker(java.lang.Integer, java.lang.String,
     * java.lang.String, boolean)
     */
    @Override
    public String updateVanker(Integer id, String name, String phone, Boolean isvalid) {
        // TODO Auto-generated method stub
        if (StringUtil.isZearoOrNull(id))
            return "id 不能为空";

        if (Strings.isNullOrEmpty(name))
            return "名称不能为空";
        
        if (Strings.isNullOrEmpty(phone))
            return "手机不能为空";

        Vanker vanker = vankerDao.selectByPrimaryKey(id);
        if (vanker == null)
            return "万小二不存在";

        vanker.setName(name);
        vanker.setPhone(phone);
        vanker.setIsvalid(isvalid==null?true:false);
        int row = vankerDao.updateByPrimaryKey(vanker);
        return row > 0 ? "ok" : "修改失败";
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.VankerService#getVanker(java.lang.Integer)
     */
    @Override
    public Vanker getVanker(Integer id) {
        // TODO Auto-generated method stub
        if (StringUtil.isZearoOrNull(id))
            return null;
       return vankerDao.selectByPrimaryKey(id);
    }

}
